<?php
declare (strict_types = 1);

namespace app\model;

use app\BaseModel;

/**
 * 提现记录模型
 */
class WithdrawRecord extends BaseModel
{
    // 设置表名
    protected $name = 'withdraw_records';
    
    // 设置字段信息
    protected $schema = [
        'id'              => 'int',
        'user_id'         => 'int',
        'amount'          => 'float',
        'wechat_qrcode'   => 'string',
        'status'          => 'int',
        'remark'          => 'string',
        'admin_id'        => 'int',
        'audit_time'      => 'datetime',
        'create_time'     => 'datetime',
        'update_time'     => 'datetime',
    ];
    
    // 设置自动时间戳
    protected $autoWriteTimestamp = true;
    
    /**
     * 获取状态文本
     */
    public function getStatusTextAttr()
    {
        $statuses = [
            0 => '待审核',
            1 => '已通过',
            2 => '已拒绝'
        ];
        return $statuses[$this->status] ?? '未知';
    }
    
    /**
     * 获取提现用户
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
    
    /**
     * 获取审核管理员
     */
    public function admin()
    {
        return $this->belongsTo(User::class, 'admin_id', 'id');
    }
    
    /**
     * 待审核提现
     */
    public static function getPending()
    {
        return self::where('status', 0)->order('create_time', 'asc');
    }
    
    /**
     * 审核通过
     */
    public function approve($adminId, $remark = '')
    {
        return $this->save([
            'status' => 1,
            'admin_id' => $adminId,
            'remark' => $remark,
            'audit_time' => date('Y-m-d H:i:s')
        ]);
    }
    
    /**
     * 审核拒绝
     */
    public function reject($adminId, $remark = '')
    {
        return $this->save([
            'status' => 2,
            'admin_id' => $adminId,
            'remark' => $remark,
            'audit_time' => date('Y-m-d H:i:s')
        ]);
    }
} 